package com.xes.recognitiondemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class HandWriteView extends View {
    public static final int DEFAULT_STROKE_WIDTH = 90;
    private Paint mPaint;
    private int mCurrX, mCurrY;
    private Path mAllPath; //记录全部path信息
    private Path mOnePath; //记录一笔画的path信息
    private OnWriteListener mDrawListener;

    public HandWriteView(Context context) {
        super(context);
        init();
    }

    public HandWriteView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HandWriteView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mAllPath = new Path();
        mOnePath = new Path();
        setBackgroundColor(Color.BLACK);
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
    }

    public void setStrokeSize(int px) {
        mPaint.setStrokeWidth(px);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawPath(mAllPath, mPaint);
    }

    public void reverse() {
        if (mPaint.getColor() == Color.WHITE) {
            mPaint.setColor(Color.BLACK);
            setBackgroundColor(Color.WHITE);
        } else {
            mPaint.setColor(Color.WHITE);
            setBackgroundColor(Color.BLACK);
        }
        mAllPath.reset();
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mCurrX = (int) event.getX();
        mCurrY = (int) event.getY();

        boolean isHandsUp = false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 按下时，重置
                mOnePath.reset();
                mAllPath.moveTo(mCurrX, mCurrY);
                mOnePath.moveTo(mCurrX, mCurrY);
                if (mDrawListener != null) {
                    mDrawListener.onWriteStart();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                isHandsUp = true; //抬手了
                break;
            default:
                break;
        }

        updateDrawHandWrite();

        if (isHandsUp) {
            if (mDrawListener != null) {
                Path path = new Path(mOnePath);
                mDrawListener.onWritePause(path);
            }
            mOnePath.reset();
        }
        return true;
    }

    private void updateDrawHandWrite(){
        // 给path添加线条
        mOnePath.lineTo(mCurrX, mCurrY);
        mAllPath.lineTo(mCurrX, mCurrY);

        invalidate();
    }

    public void clearDraw(){
        mAllPath.reset();
        invalidate();
    }

    public Bitmap getCurrentBitmap(){
        int width = getWidth();
        int height = getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPath(mAllPath, mPaint);
        return bitmap;
    }

    public void setOnDrawListener(OnWriteListener listener) {
        this.mDrawListener = listener;
    }

    public interface OnWriteListener {
        void onWriteStart();
        void onWritePause(Path path);
    }
}
